package teamGiga.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import teamGiga.bean.PageBean;
import teamGiga.bean.ParamBean;
import teamGiga.bean.QnaBean;
import teamGiga.dao.MovieDao;
import action.servlet.ActionForWard;

public class QnaListAction extends Action{
	private String path;
	private boolean redirect;
	private List<QnaBean>list;
	private PageBean pageBean;
	

	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;	}
	
	public List<QnaBean> getList() {
		return list;
	}
	public void setList(List<QnaBean> list) {
		this.list = list;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public QnaListAction() {
		super();
	}
	public QnaListAction(String path, boolean redirect) {
		super();
		this.path = path;
		this.redirect = redirect;
	}
	
	@Override
	public ActionForWard execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String search = request.getParameter("search");
		String data = request.getParameter("data");
		int page = Integer.parseInt(request.getParameter("page"));
		int pageScale=5;
		int totalRow=0;
		HashMap<String, String>map = new HashMap<String, String>();
		if(search!=null && data!=null){
			map.put("search", search);
			map.put("data", data);
			try {
				totalRow=MovieDao.getTotalRow(map);				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}else {
			try {
				totalRow=MovieDao.getTotalRow(null);				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		int totalPage=(totalRow%pageScale)==0?(totalRow/pageScale):(totalRow/pageScale)+1;
		if(totalRow==0) totalPage=1;
		
		int currentPage=page;
		
		int start = 1+(currentPage-1)*pageScale;
		int end = pageScale+(currentPage-1)*pageScale;		
		int currentBlock = currentPage%pageScale==0?currentPage/pageScale:(currentPage/pageScale)+1;
		int startPage = 1+(currentBlock-1)*pageScale;
		int endPage = pageScale+(currentBlock-1)*pageScale;
		
		if(totalPage<=endPage){
			endPage = totalPage;
		}
		HashMap<Object, Object>mlist = new HashMap<Object, Object>();
		if(search!=null && data!=null){
			mlist.put("search", search);
			mlist.put("data", data);
			mlist.put("start", start);
			mlist.put("end", end);
			
			try {
				list=MovieDao.getQna(mlist);		
				request.setAttribute("qna", MovieDao.getQna(mlist));				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}else {			
			mlist.put("start", start);
			mlist.put("end", end);
			try {
				list = MovieDao.getQna(mlist);
				request.setAttribute("qna", MovieDao.getQna(mlist));				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}

		}
		this.pageBean = new PageBean(startPage, endPage, currentPage, totalPage, currentBlock, search, data);
		request.setAttribute("pageBean", pageBean);
		
		
		return new ActionForWard(path, redirect);
	}
	
}
